package com.ktgj.customer.controller;


import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.BusinessOrder;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.CustomerCard;
import com.ktgj.customer.service.ICustomerCardService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 会员卡Controller
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@ControllerAdvice
@RestController
@RequestMapping("/customerCard")
public class CustomerCardController extends BaseController
{
    @Autowired
    private ICustomerCardService customerCardService;

    /**
     * 查询会员卡列表
     */
    @PreAuthorize(hasPermi = "customer:customerCard:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerCard customerCard)
    {
        startPage();
        List<CustomerCard> list = customerCardService.selectCustomerCardList(customerCard);
        return getDataTable(list);
    }

    /**
     * 导出会员卡列表
     */
    @PreAuthorize(hasPermi = "customer:customerCard:export")
    @Log(title = "会员卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerCard customerCard) throws IOException
    {
        List<CustomerCard> list = customerCardService.selectCustomerCardList(customerCard);
        ExcelUtil<CustomerCard> util = new ExcelUtil<CustomerCard>(CustomerCard.class);
        util.exportExcel(response, list, "会员卡数据");
    }

    /**
     * 获取会员卡详细信息
     */
    //@PreAuthorize(hasPermi = "customer:customerCard:query")
    @GetMapping(value = "/{customerCardId}")
    public AjaxResult getCustomerCardInfo(@PathVariable("customerCardId") Long customerCardId)
    {
        return AjaxResult.success(customerCardService.selectCustomerCardById(customerCardId));
    }

    /**
     * 获取会员卡详细信息
     * @param cardCode
     * @return
     */
    @PostMapping("/selectCustomerCard")
    public CustomerCard selectCustomerCard(@RequestParam("cardCode") String cardCode) {
        return customerCardService.selectCustomerCard(cardCode);
    }

    /**
     * 新增会员卡
     */
    @PreAuthorize(hasPermi = "customer:customerCard:add")
    @Log(title = "会员卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerCard customerCard)
    {
        return toAjax(customerCardService.insertCustomerCard(customerCard));
    }

    /**
     * 修改会员卡
     */
    @PreAuthorize(hasPermi = "customer:customerCard:edit")
    @Log(title = "会员卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerCard customerCard)
    {
        return toAjax(customerCardService.updateCustomerCard(customerCard));
    }

    /**
     * 删除会员卡
     */
    @PreAuthorize(hasPermi = "customer:customerCard:remove")
    @Log(title = "会员卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerCardIds}")
    public AjaxResult remove(@PathVariable Long[] customerCardIds)
    {
        return toAjax(customerCardService.deleteCustomerCardByIds(customerCardIds));
    }

    /**
     * 查询vip通道订单(远程)
     * */
    @PostMapping("/selectVipOrder")
    public CustomerCard selectVipOrder(@RequestParam("customerId") Long customerId){
        return customerCardService.selectVipOrder(customerId);
    }

    /**
     * 查询卡号（远程）
     * @param customerId
     * @return
     */
    @PostMapping("/queryCardIdByCustomerId")
    public List<CustomerCard> queryCardIdByCustomerId(@RequestParam("customerId") Long customerId) {
        List<CustomerCard> customerCardList = customerCardService.queryCardIdByCustomerId(customerId);
        return customerCardList;
    }

    /**
     * 售卡量统计
     * */
    @PostMapping("/statisticsCardSales")
    public AjaxResult statisticsCardSales(@RequestBody CardParameterVo cardParameterVo){
        List<StatisticsCardSalesVo> statisticsCardSalesVoList = customerCardService.statisticsCardSales(cardParameterVo);
        if (statisticsCardSalesVoList != null && statisticsCardSalesVoList.size() > 0){
            return AjaxResult.success(statisticsCardSalesVoList);
        }
        else {
            return AjaxResult.error("售卡数量为空");
        }
    }

    /**
     * 退卡列表
     * */
    @PostMapping("/checkOutList")
    public AjaxResult checkOutList(@RequestBody CardParameterVo cardParameterVo) {
        List<CheckOutVo> checkOutVoList = customerCardService.checkOutList(cardParameterVo);
        if (checkOutVoList != null && checkOutVoList.size() > 0){
            return AjaxResult.success(checkOutVoList);
        }
        else {
            return AjaxResult.error("退卡列表为空");
        }
    }

    /**
     * 续费列表
     * */
    @PostMapping("/renewalList")
    public AjaxResult renewalList(@RequestBody CardParameterVo cardParameterVo) {
        List<RenewalVo> renewalVoList = customerCardService.renewalList(cardParameterVo);
        if (renewalVoList != null && renewalVoList.size() > 0){
            return AjaxResult.success(renewalVoList);
        }
        else {
            return AjaxResult.error("购卡列表为空");
        }
    }

    /**
     * 查询会员的所有会员卡
     * */
    @PostMapping("/customerCardList")
    public CustomerCard customerCardList(@RequestBody Map<String, Object> map) {
        if (map.get("customerId") != null && !map.get("customerId").equals("")){
            Long customerId = Long.valueOf(map.get("customerId").toString());
            CustomerCard customerCard = customerCardService.customerCardList(customerId);
            return customerCard;
        }
        else {
            return null;
        }
    }
    /**
     * 查询数据每个部门的售卡条数
     * @param cardSaleRecords
     * @return
     */
    @PostMapping("/selectCardSaleNum")
    public AjaxResult selectCardSaleNum(@RequestBody CardSaleRecords cardSaleRecords){
        List<CardSaleRecords> list = customerCardService.selectCardSaleNum(cardSaleRecords);
        return AjaxResult.success(list);
    }
    /**
     * 查询单个部门的售卡记录
     * @param cardSaleRecords
     * @return
     */
    @PostMapping("/selectCardSaleRecords")
    public TableDataInfo selectCardSaleRecords(@RequestBody CardSaleRecords cardSaleRecords){
        startPage();
        List<BusinessOrder> list=customerCardService.selectCardSaleRecords(cardSaleRecords);
        return getDataTable(list);
    }

    /**
     * 退卡详情
     * */
    @PostMapping("/cardRefundDetails")
    public AjaxResult cardRefundDetails(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        CardRefundDetailsVo cardRefundDetailsVo;
        try {
            cardRefundDetailsVo = customerCardService.cardRefundDetails(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(cardRefundDetailsVo);
    }

    /**
     * 修改退卡详情信息
     * */
    @PostMapping("/updateCardRefund")
    public AjaxResult updateCardRefund(HttpServletRequest request, @RequestBody CardRefundDetailsVo cardRefundDetailsVo){
        int i;
        try {
            i = customerCardService.updateCardRefund(request, cardRefundDetailsVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }
}
